package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.OvertimeApplications;
import com.ruoyi.system.service.IOvertimeApplicationsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 加班Controller
 */
@RestController
@RequestMapping("/system/overtimeapply")
public class OvertimeApplicationsController extends BaseController
{
    @Autowired
    private IOvertimeApplicationsService overtimeApplicationsService;

    /**
     * 查询加班列表
     */
    @PreAuthorize("@ss.hasPermi('system:overtimeapply:list')")
    @GetMapping("/list")
    public TableDataInfo list(OvertimeApplications overtimeApplications) {
        Long currentUserId = SecurityUtils.getUserId();
        startPage();
        List<OvertimeApplications> list = overtimeApplicationsService.selectOvertimeApplicationsListByRole(overtimeApplications, currentUserId);
        // 添加用户姓名信息
        list.forEach(item -> {
            if (item.getUserName() == null) {
                item.setUserName(overtimeApplicationsService.getUserNameById(item.getUserId()));
            }
            if (item.getApproveName() == null && item.getApproveid() != null) {
                item.setApproveName(overtimeApplicationsService.getUserNameById(item.getApproveid()));
            }
        });
        return getDataTable(list);
    }

    /**
     * 获取本月加班统计
     */
    @PreAuthorize("@ss.hasPermi('system:overtimeapply:list')")
    @GetMapping("/monthStats")
    public AjaxResult getMonthStats(Long userId) {
        // 自动获取当前年月
        java.util.Calendar cal = java.util.Calendar.getInstance();
        int month = cal.get(java.util.Calendar.MONTH) + 1;
        int year = cal.get(java.util.Calendar.YEAR);
        return success(overtimeApplicationsService.getMonthOvertimeStats(userId, month, year));
    }

    /**
     * 导出加班列表
     */
    @PreAuthorize("@ss.hasPermi('system:overtimeapply:export')")
    @Log(title = "加班", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OvertimeApplications overtimeApplications)
    {
        List<OvertimeApplications> list = overtimeApplicationsService.selectOvertimeApplicationsList(overtimeApplications);
        ExcelUtil<OvertimeApplications> util = new ExcelUtil<OvertimeApplications>(OvertimeApplications.class);
        util.exportExcel(response, list, "加班数据");
    }

    /**
     * 获取加班详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:overtimeapply:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        OvertimeApplications overtimeApplications = overtimeApplicationsService.selectOvertimeApplicationsById(id);
        // 添加用户姓名和审批人姓名
        if (overtimeApplications != null) {
            overtimeApplications.setUserName(overtimeApplicationsService.getUserNameById(overtimeApplications.getUserId()));
            if (overtimeApplications.getApproveid() != null) {
                overtimeApplications.setApproveName(overtimeApplicationsService.getUserNameById(overtimeApplications.getApproveid()));
            }
        }
        return success(overtimeApplications);
    }

    /**
     * 新增加班
     */
    @PreAuthorize("@ss.hasPermi('system:overtimeapply:add')")
    @Log(title = "加班", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OvertimeApplications overtimeApplications)
    {
        // 设置默认状态为待审批
        if (overtimeApplications.getStatus() == null) {
            overtimeApplications.setStatus("PENDING");
        }
        // 设置默认审批结果说明
        if (overtimeApplications.getResult() == null) {
            overtimeApplications.setResult("审批还未通过，请耐心等待审批结果");
        }
        return toAjax(overtimeApplicationsService.insertOvertimeApplications(overtimeApplications));
    }

    /**
     * 修改加班
     */
    @PreAuthorize("@ss.hasPermi('system:overtimeapply:edit')")
    @Log(title = "加班", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OvertimeApplications overtimeApplications)
    {
        return toAjax(overtimeApplicationsService.updateOvertimeApplications(overtimeApplications));
    }

    /**
     * 删除加班
     */
    @PreAuthorize("@ss.hasPermi('system:overtimeapply:remove')")
    @Log(title = "加班", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(overtimeApplicationsService.deleteOvertimeApplicationsByIds(ids));
    }

    //查找经理下拉框
    @PreAuthorize("@ss.hasPermi('system:overtimeapply:add')")
    @GetMapping("/approvers/{userId}")
    public AjaxResult getDeptManagers(@PathVariable Long userId) {
        return success(overtimeApplicationsService.selectDeptManagersByUserId(userId));
    }
}